package com.wonders.bigdata.utils;

import org.apache.commons.io.IOUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Description: <br>
 *
 * @author: XB
 * @date: 2023/2/1 13:54
 */
public class WordUtils {
    /**
     * 替换字符串中的特殊字符
     *
     * @param str
     * @return
     */
    public static String replaceStr(String str) {
        if (!StringUtils.isEmpty(str)) {
            //替换其他特殊字符
            if (!str.contains("<w:br />")) {
                if (!str.contains("&amp;")) {
                    str = str.replace("&", "&amp;");
                }
                if (!str.contains("&lt;")) {
                    str = str.replace("<", "&lt;");
                }
                if (!str.contains("&gt;")) {
                    str = str.replace(">", "&gt;");
                }
            }
            //替换回车符
            str = str.replace("\n", "<w:br />");
            return str;
        }
        return str;
    }

    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();

        List<Field> declaredFields = new ArrayList<>() ;
        Class tempClass = obj.getClass();
        while (tempClass != null) {//当父类为null的时候说明到达了最上层的父类(Object类).
            declaredFields.addAll(Arrays.asList(tempClass .getDeclaredFields()));
            tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
        }

        for (Field field : declaredFields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            if (value == null) {
                if (field.getType().equals(String.class)) {
                    value = "";
                } else if (field.getType().equals(Integer.class)) {
                    value = 99;
                }
            }
            map.put(fieldName, value);
        }
        return map;
    }

    /**
     * 将map中 对应的空值替换为 ""
     *
     * @param map
     * @return
     */
    public static Map<String, Object> checkNull(Map<String, Object> map) {
        if (Objects.isNull(map)) {
            return map;
        }
        map.forEach((key, value) -> {
            if (Objects.isNull(map.get(key))) {
                map.put(key, "");
            }
        });
        return map;
    }

    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Object checkObjNull(Object obj) {
        Class<?> clazz = obj.getClass();
        try {
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                Object value = field.get(obj);
                if (value == null) {
                    if (field.getType().equals(String.class)) {
                        value = "";
                    } else if (field.getType().equals(Integer.class)) {
                        value = 99;
                    }
                } else {
                    if (field.getType().equals(String.class)) {
                        value = replaceStr(value.toString());
                    }
                }

                ReflectionUtils.setField(field,obj,value);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static String streamToBase64String(InputStream inputStream) {
        if (!Objects.nonNull(inputStream)) {
            return null;
        }

        try {
            byte[] bytes = IOUtils.toByteArray(inputStream);
            return Base64.getEncoder().encodeToString(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
